package com.slt.android.customview.d2canvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 2d绘图基础
 * Created by dp on 2017/12/21.
 */

public class d2CanvasView extends View {
    Paint mPaint;

    public d2CanvasView(Context context) {
        super(context);
        init(context);
    }

    public d2CanvasView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public d2CanvasView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//true表示去锯齿
        mPaint.setColor(Color.BLUE);//设置颜色
        //Paint.Style.STROKE 只绘制图形轮廓（描边）
        //Paint.Style.FILL 只绘制图形内容
        //Paint.Style.FILL_AND_STROKE 既绘制轮廓也绘制内容
        //STROKE和FILL_AND_STROKE的粗细是一样的,FILL最细
//        mPaint.setStyle(Paint.Style.FILL);
//        mPaint.setARGB(50 , 50 , 50 , 50);//设置画笔的argb值
//        mPaint.setAlpha(100);//设置画笔的透明度
        mPaint.setTextSize(40);//设置字体的大小
//        mPaint.setStrokeWidth(20);//设置空心边框的的宽度
    }

    int x = 50, y = 50;
    float x1 = 50, y1 = 50;
    float pts[] = {x1, y1, x1, y1 + 200,
            x1, y1 + 200, x1 + 200, y1 + 200,
            x1 + 200, y1 + 200, x1 + 50, y1 + 100,
            x1 + 50, y1 + 100, x1, y1};

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);// 把整张画布绘制成白色
//        canvas.drawPoint(x , y , mPaint);//画一个点
//        canvas.drawLine(x , y , 250 , 250 , mPaint);//画一条线 开始xy坐标，结束xy坐标
//        canvas.drawLines(pts , mPaint);//画一条线 点数组形式
        mPaint.setStyle(Paint.Style.FILL);//设置下面画的矩形为空心还是实心
//        canvas.drawRect(50 , 50 , 250 , 250 , mPaint);//画一个矩形 ，参数可以是4个坐标也可以是Rect
//        RectF mRect = new RectF(50 , 50 , 250 , 250 );
//        canvas.drawRoundRect(mRect , 30 , 30 , mPaint);//画一个圆角矩形 ，参数可以是4个坐标也可以是Rect
//        RectF mRect = new RectF(50, 50, 250, 250);
//        canvas.drawArc(50f , 50f , 250f , 250f , -60f , 180f , false , mPaint);//画圆弧或者是扇形 ，参数可以是4个坐标也可以是RectF
//        canvas.drawArc(mRect, -60f, 120f, true, mPaint);Paint.Style决定是否空心,useCenter决定画出的图形是否和圆心相连接
//        canvas.drawCircle(getWidth()/2 , getHeight()/2 , 100 , mPaint);//画圆形 ，参数是 圆心，半径，画笔
//        RectF mRect = new RectF(50, 50, 350, 250);
//        canvas.drawOval(mRect , mPaint);//画一个椭圆 ，参数可以是4个坐标也可以是RectF
//        canvas.drawText("Android工程师" , 50 , 250 , mPaint);//写文字，参数是开始x,y坐标 ，画笔
//        canvas.drawPosText();//被淘汰了
//        drawPath(canvas);//画连续的线条
    }





    private void drawPath(Canvas canvas) {
        Path path = new Path();
        path.moveTo(50 , 50);//初始点
        path.lineTo(100 , 100 );//连线
        path.lineTo(100 , 300);//连线
        path.lineTo(300 , 300);//连线
        path.lineTo(300 , 50);//连线
        path.lineTo(50 , 50);//连线
        canvas.drawPath(path , mPaint);//画连续的线条
    }
}
